#!/usr/bin/env python
# encoding: utf-8
"""
@summary: 执行程序入口
@author: hongxingfan
@since: 2014年8月7日    下午5:35:21
"""
from DealData import Deal
from SendMail import send_mail
import datetime
import os

if __name__ == "__main__":
    now = datetime.datetime.now()
    yesterday = now + datetime.timedelta(days = -1)
    yesterday = yesterday.strftime('%Y%m%d')
    mr = "hadoop jar /opt/sohuhadoop/hadoop/contrib/streaming/hadoop-streaming-0.20.2-cdh3u1.jar -input \"/user/autolog/pvlog/" + yesterday + "\" -output \"/user/autolog/tmp/fan/ModelOfPopularity_" + yesterday + "\" -mapper \"python Mapper.py\" -reducer \"python Reducer.py\" -file ./Mapper.py ./Reducer.py ./Url.py ./trimIdToModelId.txt"
    os.popen(mr)
       
    deal = Deal(host = "10.10.77.6", uname = "wanjiang", passwd = "wanjiang0310", dbname = "auto_warehouse")
    content = ""
    i = 0
    for entity in deal.getDataFromHDFS(yesterday):
        if i > 1500:
            break
        content += str(i) + "\t" + entity.corp_model + "\t" + str(entity.pv) + "\t" + str(entity.uv) + "\n"
        i += 1
    mailto_list = ["fanhx825@163.com"] 
    if send_mail(mailto_list, "车型数据排行:" + yesterday, content):  
        print "发送成功"  
    else:
        print "发送失败"